﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.SqlServerDAL;

public partial class BaseData_AreaManage_Edit : System.Web.UI.Page
{
    Area MyArea = new Area();
    Param MyP = new Param();
    string sID = String.Empty;
    string sType = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            sID = this.Request.QueryString["ParentChildID"].ToString().Trim() == "" ? "0" : this.Request.QueryString["ParentChildID"].ToString().Trim();
            sType = this.Request.QueryString["type"].ToString().Trim();
        }
        catch
        { }
        if (!this.IsPostBack)
        {
            ddlRegionDataBind();
            //绑定区域下拉框
            lstArea.Attributes.Add("onclick", "FindSelectedItems(this," + txtArea.ClientID + ");");
            this.BindlstArea();
            this.BindddlLargeArea();
            this.BindddlEquipment();
            if (sType == "2")
            {
                DataTable dt = MyArea.GetAreaTableByID(sID);
                ddlLargeArea.SelectedValue = dt.Rows[0]["LargeArea"].ToString();
                ddlEquipment1.SelectedValue = dt.Rows[0]["Equipment1"].ToString();
                ddlEquipment2.SelectedValue = dt.Rows[0]["Equipment2"].ToString();
                ddlEquipment3.SelectedValue = dt.Rows[0]["Equipment3"].ToString();
                string[] aCheckList = dt.Rows[0]["Neighbors"].ToString().Split(',');
                for (int i = 0; i < aCheckList.Length; i++)
                {
                    for (int j = 0; j < lstArea.Items.Count; j++)
                    {
                        if (lstArea.Items[j].Value == aCheckList[i])
                        {
                            lstArea.Items[j].Selected = true;
                            txtArea.Value += txtArea.Value == String.Empty ? lstArea.Items[j].Text : "," + lstArea.Items[j].Text;
                        }
                    }
                }
                this.PinYin.Text = dt.Rows[0]["PinYin"].ToString();
            }
        }
    }

    private void BindddlEquipment()
    {
        ddlEquipment1.DataSource = MyP.GetCodeByParentCode("4");
        ddlEquipment1.DataTextField = "Name";
        ddlEquipment1.DataValueField = "Code";
        ddlEquipment1.DataBind();
        ddlEquipment1.Items.Insert(0, "请选择...");
        ddlEquipment1.Items[0].Value = "";

        ddlEquipment2.DataSource = MyP.GetCodeByParentCode("4");
        ddlEquipment2.DataTextField = "Name";
        ddlEquipment2.DataValueField = "Code";
        ddlEquipment2.DataBind();
        ddlEquipment2.Items.Insert(0, "请选择...");
        ddlEquipment2.Items[0].Value = "";

        ddlEquipment3.DataSource = MyP.GetCodeByParentCode("4");
        ddlEquipment3.DataTextField = "Name";
        ddlEquipment3.DataValueField = "Code";
        ddlEquipment3.DataBind();
        ddlEquipment3.Items.Insert(0, "请选择...");
        ddlEquipment3.Items[0].Value = "";
    }

    private void BindddlLargeArea()
    {
        ddlLargeArea.DataSource = MyP.GetCodeByParentCode("3");
        ddlLargeArea.DataTextField = "Name";
        ddlLargeArea.DataValueField = "Code";
        ddlLargeArea.DataBind();
        ddlLargeArea.Items.Insert(0, "请选择...");
        ddlLargeArea.Items[0].Value = "";
    }

    /// <summary>
    /// 绑定区域数据
    /// </summary>
    private void BindlstArea()
    {
        if (sType == "1")
        {
            lstArea.DataSource = MyArea.GetAreaByParent(sID);
        }
        else
        {
            lstArea.DataSource = MyArea.GetAreaByID(sID);
        }
        lstArea.DataTextField = "Name";
        lstArea.DataValueField = "ID";
        lstArea.DataBind();
    }

    /// <summary>
    /// 区域下拉框
    /// </summary>
    protected void ddlRegionDataBind()
    {
        ddlRegionid.DataSource = MyArea.GetAreaInfos(string.Empty);
        ddlRegionid.DataBind();
        ddlRegionid.Items.Insert(0, "顶级区域");
        ddlRegionid.Items[0].Value = "0";
    }

    protected void GrantBtn1_ServerClick(object sender, EventArgs e)
    {
        string sEditType = hidEditType.Value.ToString().Trim();
        string sID = HAreaID.Value.ToString().Trim();
        string sAreaName = AreaName.Text.ToString().Trim();

        int iParentID = System.Convert.ToInt32(ddlRegionid.SelectedValue.ToString().Trim());
        string sCreator = "0";
        int iOrgID = 0, iPersID = 0;
        try
        {
            sCreator = (string)Session["Baimasoft_User_LogonID"];
            iOrgID = System.Convert.ToInt32((string)Session["Baimasoft_Dept_ID"]);
            iPersID = System.Convert.ToInt32((string)Session["Baimasoft_Pers_ID"]);
        }
        catch
        {
        }
        string sNeighbors = String.Empty;
        for (int j = 0; j < lstArea.Items.Count; j++)
        {
            if (lstArea.Items[j].Selected)
                sNeighbors += lstArea.Items[j].Value.Trim() + ",";
        }
        string sLargeArea = ddlLargeArea.SelectedValue.Trim() == String.Empty ? "0" : ddlLargeArea.SelectedValue.Trim();
        sNeighbors = sNeighbors == String.Empty ? "" : sNeighbors.Substring(0, sNeighbors.LastIndexOf(","));
        string sEquipment1 = ddlEquipment1.SelectedValue.Trim() == String.Empty ? "0" : ddlEquipment1.SelectedValue.Trim();
        string sEquipment2 = ddlEquipment2.SelectedValue.Trim() == String.Empty ? "0" : ddlEquipment2.SelectedValue.Trim();
        string sEquipment3 = ddlEquipment3.SelectedValue.Trim() == String.Empty ? "0" : ddlEquipment3.SelectedValue.Trim();
        if (sEditType == "1")
        {

            int iback = MyArea.InsertInfo(0, iParentID, sAreaName, sCreator, 1, iOrgID, iPersID, sLargeArea, sEquipment1, sEquipment2, sEquipment3, sNeighbors, PinYin.Text.Trim());
            if (iback > 0)
            {
                MyArea.UpAreaChildNumber();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                return;
            }
        }
        else if (sEditType == "2")
        {
            int iback = MyArea.UpdateAreaInfo(0, iParentID, sAreaName, sCreator, 1, iOrgID, iPersID, sID, sLargeArea, sEquipment1, sEquipment2, sEquipment3, sNeighbors, PinYin.Text.Trim());
            if (iback > 0)
            {
                MyArea.UpAreaChildNumber();

                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
            return;
        }
    }
}
